import Vue from "vue";
function formatTen(num) {
  return num > 9 ? num + "" : "0" + num;
}
const MyUtils = {
  contentIsNull: (text, key = "name") => {
    if (text) {
      return text[key] ? text[key] : "";
    }
    return "";
  },
  arrayIsNull: (array) => {
    if (!Array.isArray(array)) {
      return false;
    }
    if (array && array.length > 0) {
      return true;
    }
    return false;
  },
  isMobile() {
    let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
    return flag;
  },
  // 格式化时间
  timeFormat: (time, fmt) => {
    let date = time ? new Date(time) : new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1 > 9 ? parseInt(date.getMonth()) + 1 : "0" + parseInt(parseInt(date.getMonth()) + 1);
    let day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    let hour = parseInt(date.getHours()) > 9 ? date.getHours() : "0" + date.getHours();
    let minute = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
    let second = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
    switch (fmt) {
      case "yyyy-MM-dd hh:mm:ss":
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
      case "yyyy-MM-dd hh:mm":
        return `${year}-${month}-${day} ${hour}:${minute}`;
      case "yyyy-MM-dd hh":
        return `${year}-${month}-${day} ${hour}`;
      case "yyyy-MM-dd":
        return `${year}-${month}-${day}`;
      case "yyyy-MM":
        return `${year}-${month}`;
      case "MM-dd":
        return `${month}-${day}`;
      case "yyyy":
        return `${year}`;
      case "MM":
        return `${month}`;
      case "dd":
        return `${day}`;
      case "yyyy年MM月dd日 hh:mm:ss":
        return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
      case "yyyy年MM月dd日 hh:mm":
        return `${year}年${month}月${day}日 ${hour}:${minute}`;
      case "yyyy年MM月dd日 hh":
        return `${year}年${month}月${day}日 ${hour}`;
      case "yyyy年MM月dd日":
        return `${year}年${month}月${day}日`;
      case "yyyy年MM月":
        return `${year}年${month}月`;
      case "MM月dd日":
        return `${month}月${day}日`;
      case "yyyy年":
        return `${year}年`;
      case "MM月":
        return `${month}月`;
      case "dd日":
        return `${day}日`;
      case "yyyyMMdd":
        return `${year}${month}${day}`;
      case "[yyyy,MM,dd,hh,mm,ss]":
        return [year, month, day, hour, minute, second];
      case "Object":
        return {
          yyyy: year,
          MM: month,
          dd: day,
          hh: hour,
          mm: minute,
          ss: second,
          year,
          month,
          day,
          hour,
          minute,
          second,
        };
      default:
        return {
          yyyy: year,
          MM: month,
          dd: day,
          hh: hour,
          mm: minute,
          ss: second,
          year,
          month,
          day,
          hour,
          minute,
          second,
        };
    }
  },

  //标准时间转年月日---
  TimeYY: (date) => {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    return year + "-" + formatTen(month) + "-" + formatTen(day) + " " + formatTen(hour) + ":" + formatTen(minute) + ":" + formatTen(second);
  },

  TimeYYdAY: (date) => {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();

    let weekArray = new Array("日", "一", "二", "三", "四", "五", "六");
    let week = weekArray[new Date(date).getDay()];
    let data = {
      year: year,
      month: formatTen(month),
      day: formatTen(day),
      week: week,
    };
    return data;
  },
  //倒计时
  daojishi: (endTime, startTime) => {
    let date = endTime - startTime; //时间差
    let mmsec = date % 1000; //所余毫秒数
    let seconds = Math.floor((date / 1000) % 60); //所余秒数
    let minutes = Math.floor((date / 1000 / 60) % 60); //所余分钟数
    let hour = Math.floor((date / 1000 / 60 / 60) % 24); //所余时钟数
    let day = Math.floor(date / 1000 / 60 / 60 / 24); //天数
    return {
      day: day,
      hour: hour,
      minutes: minutes,
      seconds: seconds,
      mmsec: mmsec,
    };
  },
  //计算后多少分钟之后的时间
  addMinutes: (date, minutes) => {
    minutes = parseInt(minutes);
    let interTimes = minutes * 60 * 1000;
    interTimes = parseInt(interTimes);
    return new Date(Date.parse(date) + interTimes);
  },

  //标准时间转年月日
  TimeDate: (date) => {
    console.log(date, 22222);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    return year.toString().substr(2, 2) + formatTen(month);
  },
  //获取日期的前十天
  GetDateStr: (AddDayCount) => {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1; //获取当前月份的日期，不足10补0
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
    return y + "-" + m + "-" + d;
  },

  //时间戳转时间
  // 时间戳转换成时间
  timestampToTime: (cjsj) => {
    var date = new Date(cjsj); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + "-";
    var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    var h = date.getHours() + ":";
    var m = date.getMinutes() + ":";
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
  },

  timestampToTimes: (cjsj) => {
    var date = new Date(cjsj); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + "-";
    var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
    var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
    var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    return Y + M + D + h + m;
  },
};

export default MyUtils;

// export function array2Tree(array) {
// 	let data = deepCopy(array)
// 	let result = []
// 	let map = {}
// 	if (!Array.isArray(data)) {//验证data是不是数组类型
// 		return []
// 	}
// 	data.forEach(item => {//建立每个数组元素id和该对象的关系
// 		map[item.id] = item //这里可以理解为浅拷贝，共享引用
// 	})
// 	data.forEach(item => {
// 		let parent = map[item.pid] //找到data中每一项item的爸爸
// 		if (parent) {//说明元素有爸爸，把元素放在爸爸的children下面
// 			(parent.children || (parent.children = [])).push(item)
// 		} else {//说明元素没有爸爸，是根节点，把节点push到最终结果中
// 			result.push(item) //item是对象的引用
// 		}
// 	})
// 	return result //数组里的对象和data是共享的
// }

// export function tree2Array(node) {
// 	let queue= deepCopy(node)
// 	let data = [] //返回的数组结构
// 	while (queue.length !== 0) { //当队列为空时就跳出循环
// 		let item = queue.shift() //取出队列中第一个元素
// 		if(item){
// 			data.push({
// 				val: item.val,
// 				key: item.key
// 			})
// 		}
// 		let children = item.children // 取出该节点的孩子
// 		if (children) {
// 			for (let i = 0; i < children.length; i++) {
// 				queue.push(children[i]) //将子节点加入到队列尾部
// 			}
// 		}
// 	}
// 	return data
// }

// function deepCopy(obj){
// 	// 深度复制数组
// 	if(Object.prototype.toString.call(obj) === "[object Array]"){
// 		const object=[];
// 		for(let i=0;i<obj.length;i++){
// 			object.push(deepCopy(obj[i]))
// 		}
// 		return object
// 	}
// 	// 深度复制对象
// 	if(Object.prototype.toString.call(obj) === "[object Object]"){
// 		const object={};
// 		for(let p in obj){
// 			object[p]=obj[p]
// 		}
// 		return object
// 	}
// }

// // 身份证手机号脱敏处理
// export function desensitization(str,beginLen = 4,endLen = -4){
// 	if(str){
// 		let len = str.length;
// 		let firstStr = str.substr(0,beginLen);
// 		let lastStr = str.substr(endLen);
// 		let middleStr = str.substring(beginLen, len-Math.abs(endLen)).replace(/[\s\S]/ig, '*');
// 		let tempStr = firstStr + middleStr + lastStr;
// 		return tempStr;
// 	} else {
// 		return '';
// 	}
// }

// 日期格式化
// export function parseTime(time, pattern) {
// 	if (arguments.length === 0 || !time) {
// 		return null
// 	}
// 	const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
// 	let date
// 	if (typeof time === 'object') {
// 		date = time
// 	} else {
// 		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
// 			time = parseInt(time)
// 		} else if (typeof time === 'string') {
// 			time = time.replace(new RegExp(/-/gm), '/');
// 		}
// 		if ((typeof time === 'number') && (time.toString().length === 10)) {
// 			time = time * 1000
// 		}
// 		date = new Date(time)
// 	}
// 	const formatObj = {
// 		y: date.getFullYear(),
// 		m: date.getMonth() + 1,
// 		d: date.getDate(),
// 		h: date.getHours(),
// 		i: date.getMinutes(),
// 		s: date.getSeconds(),
// 		a: date.getDay()
// 	}
// 	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
// 		let value = formatObj[key]
// 		// Note: getDay() returns 0 on Sunday
// 		if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
// 		if (result.length > 0 && value < 10) {
// 			value = '0' + value
// 		}
// 		return value || 0
// 	})
// 	return time_str
// }
